package com.hts.utils.string;

import com.alibaba.fastjson.JSONObject;

/**
 * @author hs
 * @version 1.0
 * @date: 2023/6/27
 */
public class StringFormat {
    /**
     * 大括号占位符号
     * @param message
     * @param messages
     * @return
     */
    public static String formatCurlyBraces(String message, Object... messages) {
        for (Object o : messages) {
            message = message.replaceFirst("\\{\\}", o instanceof String ? o.toString() : JSONObject.toJSONString(o));
        }
        return message;
    }

    public static String formatCurlyBracesIndex(String message, Object... messages) {
        int i = message.indexOf("{}");
        StringBuilder append = new StringBuilder();
        if (i == -1) {
            return message;
        }
        int j = 0;
        while (i != -1) {
            append.append(message, 0, i);
            if (j < messages.length) {
                append.append(messages[j].toString());
                j++;
            }
            message = message.substring(i + 2);
            i = message.indexOf("{}");
            if (i == -1) {
                append.append(message);
            }
        }
        return append.toString();
    }



}
